package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.view.View
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.entity.newtv.TvUserInfo
import com.ellricky.baselib.util.jump
import kotlinx.android.synthetic.main.activity_new_tv_login.*

class NewTvLoginActivity : BaseActivity<Void, BasePresenter<Void>>() {

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_login
    }

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun initUIView() {
        tv_login.setOnClickListener(this)
        tv_cancel.setOnClickListener(this)
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.tv_login -> doLogin()
            R.id.tv_cancel -> finish()
        }
    }

    private fun doLogin() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "user_login",
            "tel" to et_mobile.text.toString(),
            "pwd" to et_password.text.toString()
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEntityCallback<TvUserInfo>(this) {
                override fun onSuccess(data: TvUserInfo, msg: String) {
                    LocalCacheUtil.saveTvUser(data)
                    jump<NewTvUserInfoActivity>()
                    finish()
                }
            }, this)
    }
}